package org.jash.learnmvp.user;

import org.jash.learnmvp.mvp.BasePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;

public class RegisterPresenter extends BasePresenter<ConStruct.IRegisterView> implements ConStruct.IRegisterPresenter {
    private ConStruct.IRegisterModel model = new RegisterModel();
    @Override
    public void loadCode(String phone) {
        disposable.add(model.getRCode(phone)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(res -> {
                    if (isAttach()) {
                        if (res.getCode() == 0) {
                            getWhoView().codeSuccess(res.getData());
                        } else {
                            getWhoView().failure(new RuntimeException(res.getMsg()));
                        }
                    }
                }, t -> {
                    if (isAttach()) {
                        getWhoView().failure(t);
                    }
                }));
    }

    @Override
    public void register(String username, String password, String phone) {
        disposable.add(model.register(username, password, phone)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(res -> {
                    if (isAttach()) {
                        if (res.getCode() == 0) {
                            getWhoView().registerSuccess(res.getMsg());
                        } else {
                            getWhoView().failure(new RuntimeException(res.getMsg()));
                        }
                    }
                }, t -> {
                    if (isAttach()) {
                        getWhoView().failure(t);
                    }
                }));
    }

    @Override
    public void verify(String phone, String code) {
        disposable.add(model.verify(phone, code)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(res -> {
                    if (isAttach()) {
                        if (res.getCode() == 0) {
                            getWhoView().verifySuccess(res.getMsg());
                        } else {
                            getWhoView().failure(new RuntimeException(res.getMsg()));
                        }
                    }
                }, t -> {
                    if (isAttach()) {
                        getWhoView().failure(t);
                    }
                }));
    }
}
